package com.carlinfo.stu_ques.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.common.util.DateUtil;
import com.carlinfo.stu_ques.demo.pojo.King;
import com.carlinfo.stu_ques.demo.service.IDemoService;
import com.carlinfo.stu_ques.demo.service.impl.DemoServiceImpl;
import com.carlinfo.stu_ques.users.pojo.Level;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.IUsersService;
import com.carlinfo.stu_ques.users.service.impl.UsersServiceImpl;

/**
 * 客户的主程序 用户运行的时候,只有这一个main方法;
 */
public class UsersClient
{
	private IUsersService usersService = new UsersServiceImpl();
	Map<String, Object> map = new HashMap<String, Object>();
	DateUtil dateUtil = new DateUtil();

	public void userLaunch(BufferedReader br)
	{
		try
		{
			while (true)
			{
				System.out.println("==学生操作==按n退出,h进入帮助界面");
				String line = br.readLine();
				line = line.trim();
				if ("n".equalsIgnoreCase(line))
				{
					System.out.println("你退出了学生操作,你已进入主菜单！");
					break;
				} else if ("h".equalsIgnoreCase(line))
				{
					help();
				} else if ("1".equalsIgnoreCase(line))
				{
					selectListStudentService();
				} else if ("2".equalsIgnoreCase(line))
				{
					saveOneStudentService(br);
				} else if ("3".equalsIgnoreCase(line))
				{
					updateOneStudentService(br);
				} else if ("4".equalsIgnoreCase(line))
				{
					deleteOneStudentService(br);
				} else if ("5".equalsIgnoreCase(line))
				{
					selectOneStudentService(br);
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void userLaunch2(BufferedReader br)
	{

		try
		{
			while (true)
			{
				System.out.println("==等级操作==按n退出,h进入帮助界面");
				String line = br.readLine();
				line = line.trim();
				if ("n".equalsIgnoreCase(line))
				{
					System.out.println("你退出了等级操作,你已进入主菜单");
					break;
				} else if ("h".equalsIgnoreCase(line))
				{
					help1();
				} else if ("1".equalsIgnoreCase(line))
				{
					selectListLevelService();

				} else if ("2".equalsIgnoreCase(line))
				{
					saveOneLevelService(br);
				} else if ("3".equalsIgnoreCase(line))
				{
					updateOneLevelService(br);
				} else if ("4".equalsIgnoreCase(line))
				{
					deleteOneLevelService(br);
				} else if ("5".equalsIgnoreCase(line))
				{
					selectOneLevelService(br);
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void updateOneLevelService(BufferedReader br)
	{
		Level level = this.selectOneLevelService(br);
		try
		{
			if (level != null)
			{
				System.out.println("请依次输入等级的相关信息:名字,初始分数,结束分数");
				System.out.println("示例:铂金|76|80");
				String line = br.readLine();
				line = line.trim();
				String[] lines = line.split("\\|");
				if (lines.length != 3)
				{
					System.out.println("你输入的格式有问题");
					return;
				}
				level.setName(lines[0]);
				level.setFinalMarks(Integer.valueOf(lines[2]));
				level.setStartMarks(Integer.valueOf(lines[1]));
				level.setUpdateTime(new Date());
				System.out.println("成功修改了,id:" + level.getId());
			} else
			{
				return;
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.updateOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	private void deleteOneLevelService(BufferedReader br)
	{
		Level level = this.selectOneLevelService(br);
		if (level != null)
		{
			this.usersService.deleteOneLevelService(level);
			System.out.println("你已经删除了一条,id:" + level.getId());
		} else
		{
			return;
		}

	}

	private Level selectOneLevelService(BufferedReader br)
	{
		System.out.println("请输入你要查询的id");
		try
		{
			String line = br.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			Level level = this.usersService.selectOneLevelService(map);
			if (level != null)
			{
				System.out.println(this.usersService.levelTitle());
				System.out.println(level);
			} else
			{
				System.out.println("无此记录，查询失败");
			}
			return level;
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private void saveOneLevelService(BufferedReader br)
	{
		System.out.println("请依次输入等级的相关信息:名字,初始分数,结束分数");
		System.out.println("示例:铂金|76|80");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 3)
			{
				System.out.println("你输入的格式有问题");
				return;
			}
			Level level = new Level();
			level.setName(lines[0]);
			level.setFinalMarks(Integer.valueOf(lines[2]));
			level.setStartMarks(Integer.valueOf(lines[1]));
			level.setUpdateTime(new Date());
			level.setCreateTime(new Date());
			int res = this.usersService.saveOneLevelService(level);
			System.out.println("添加成功,id:" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.saveOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	private void selectListLevelService()
	{
		List<Level> list = usersService.selectListLevelService();
		System.out.println(usersService.levelTitle());
		for (Level level : list)
		{
			System.out.println(level);
		}
	}

	private Students selectOneStudentService(BufferedReader br)
	{
		System.out.println("请输入你要查询的id");
		try
		{
			String line = br.readLine();
			map.put("id", Integer.valueOf(line.trim()));
			Students students = this.usersService.selectOneStudentService(map);
			if (students != null)
			{
				System.out.println(this.usersService.title());
				System.out.println(students);
			} else
			{
				System.out.println("无此记录，查询失败");
			}
			return students;
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.selectOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除一个学生
	 * 
	 * @param br
	 */
	private void deleteOneStudentService(BufferedReader br)
	{
		Students students = this.selectOneStudentService(br);
		if (students != null)
		{
			this.usersService.deleteOneStudentService(students);
			System.out.println("你已经删除了一条,id:" + students.getId());
		} else
		{
			return;
		}
	}

	/**
	 * 更新一个学生
	 * 
	 * @param br
	 */
	private void updateOneStudentService(BufferedReader br)
	{
		Students students = this.selectOneStudentService(br);
		try
		{
			if (students != null)
			{
				System.out.println("请依次输入学生的相关信息:姓名,性别,生日,毕业院校,分数,答对次数,答错次数");
				System.out.println("示例:张三|男|1996-12-03|北大|30|3|4");
				String line = br.readLine();
				line = line.trim();
				String[] lines = line.split("\\|");
				if (lines.length != 7)
				{
					System.out.println("你输入的格式有问题");
					return;
				}
				students.setName(lines[0]);
				students.setSex(lines[1]);
				students.setBirth(dateUtil.strToDate(lines[2]));
				students.setGraduateSchool(lines[3]);
				students.setLevel(Double.valueOf(lines[4]));
				students.setConutTrue(Integer.valueOf(lines[5]));
				students.setCountFalse(Integer.valueOf(lines[6]));
				students.setUpdateTime(new Date());
				System.out.println("成功修改了,id:" + students.getId());
			} else
			{
				return;
			}
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.updateOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个学生
	 * 
	 * @param br
	 */
	private void saveOneStudentService(BufferedReader br)
	{
		System.out.println("请依次输入学生的相关信息:姓名,性别,生日,毕业院校,分数,答对次数,答错次数");
		System.out.println("示例:张三|男|1996-12-03|北大|30|3|4");
		try
		{
			String line = br.readLine();
			line = line.trim();
			String[] lines = line.split("\\|");
			if (lines.length != 7)
			{
				System.out.println("你输入的格式有问题");
				return;
			}
			Students students = new Students();
			students.setName(lines[0]);
			students.setSex(lines[1]);
			students.setBirth(dateUtil.strToDate(lines[2]));
			students.setGraduateSchool(lines[3]);
			students.setLevel(Double.valueOf(lines[4]));
			students.setConutTrue(Integer.valueOf(lines[5]));
			students.setCountFalse(Integer.valueOf(lines[6]));
			students.setUpdateTime(new Date());
			students.setCreateTime(new Date());
			int res = this.usersService.saveOneStudentService(students);
			System.out.println("添加成功,id:" + res);
		} catch (NumberFormatException e)
		{
			System.out.println("你输入的不是整数,请重新输入");
			this.saveOneStudentService(br);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private void selectListStudentService()
	{
		System.out.println(this.usersService.title());
		List<Students> list = this.usersService.selectListStudentService();
		for (Students students : list)
		{
			System.out.println(students);
		}

	}

	public void help()
	{
		System.out.println("1:查询所有学生");
		System.out.println("2:添加学生");
		System.out.println("3:修改学生;(先输入学生id,填写要修改的内容)");
		System.out.println("4:删除学生");
		System.out.println("5:查看单个学生");
	}

	public void help1()
	{
		System.out.println("1:查询所有等级");
		System.out.println("2:添加等级");
		System.out.println("3:修改等级;(先输入等级id,填写要修改的内容)");
		System.out.println("4:删除等级");
		System.out.println("5:查看单个等级");
	}

	public void setUsersService(IUsersService usersService)
	{
		this.usersService = usersService;
	}

}
